module.exports = {
    sign($, cookie) {
        return new Promise(resolve => {
            const param = '{"activityId":"118754080","shopId":"1623185041","signType":"sign"}'
            // const js = 'wdtoken=6d419628;  uid=396895509; login_token=_EwWqqVIQdP7BkKIZopBm2HmFAePLX35QMUeSugb30PTc3ZGYpu_aki08u4pT9YXCKNtn714Ybjz4LCYWwod0_J86maZ6xSjtX-pzmxefEUNrytAzHaM04-PyfFi-c58zym4Ebx9lk_iyRXVnIjpIjbjH6YPkqYmRBp7Ffj9ymTEXSiL-8LI0LKFgqpRWFIvRpsx_jVWtrqvUiTkJJOeKMJlaiQKvdCvjhE0aavdndcdEvottLb-hJvVE8LVg6qjqwYXMVPUU'
            if(!cookie) {
                resolve({status: {code: 1, message: 'cookie 空'}})
            }
            const wdtokenArr = cookie.split(';').map(item => {
                return item.trim()
            }).filter(item => item.indexOf('wdtoken') > -1)
            if(!wdtokenArr || wdtokenArr.length == 0) {
                resolve({status: {code: 1, message: 'wdtoken 空'}})
            }
            const wdtoken = wdtokenArr[0].split('=')[1]
            $.post({
                url: $.baseUrl + `/promotion/buyer.sign.activity.sign/1.0`,
                body: `wdtoken=${wdtoken}&param=${param}`,
                headers: {...$.headers, 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': cookie},
            }, async (err, res, resp) => {
                resolve(JSON.parse(resp))
            })
        })
    },
    signTask($, cookie) {
        return new Promise(resolve => {
            const param = '{"shopId":"1623185041","buyerId":"1808363296","activityId":2,"activityType":1}'
            if(!cookie) {
                resolve({status: {code: 1, message: 'cookie 空'}})
            }
            const wdtokenArr = cookie.split(';').map(item => {
                return item.trim()
            }).filter(item => item.indexOf('wdtoken') > -1)
            if(!wdtokenArr || wdtokenArr.length == 0) {
                resolve({status: {code: 1, message: 'wdtoken 空'}})
            }
            const wdtoken = wdtokenArr[0].split('=')[1]
            $.post({
                url: $.baseUrl + `/promotion/ShopCoinTask.buyer.sendShopCoinTaskPrize/1.0`,
                body: `wdtoken=${wdtoken}&param=${param}`,
                headers: {...$.headers, 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': cookie},
            }, async (err, res, resp) => {
                resolve(JSON.parse(resp))
            })
        })
    },
    getCookieValue(cookies, key) {
        if(!cookies) {
            return null
        }
        const wdtokenArr = cookies.split(';').map(item => {
            return item.trim()
        }).filter(item => item.indexOf(key) > -1)
        if(!wdtokenArr || wdtokenArr.length == 0) {
            return null
        }
        const fArr = wdtokenArr.filter(item => item.indexOf('=') > -1)
        if(!fArr || fArr.length == 0) {
            return null
        }
        return decodeURIComponent(fArr[0].split('=')[1])
    },
    // 去重  const newArr = this.unique(arr, "id")
    unique(arr, val) {
        const res = new Map()
        return arr.filter((item) => !res.has(item[val]) && res.set(item[val], 1))
    },

    // 休眠 1 秒
    // await sleep(1000)
    sleep(delay) {
        return new Promise((resolve) => {
            setTimeout(resolve, delay)
        })
    },
    // 当前日期
    currDate(d = new Date()) {
        return d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate() < 10 ? '0' + (d.getDate()) : d.getDate())
    },
    // 当前日期时间
    currDateTime(d = new Date()) {
        return d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate() < 10 ? '0' + (d.getDate()) : d.getDate()) + ' ' + (d.getHours() < 10 ? '0' + (d.getHours()) : d.getHours()) + ':' + (d.getMinutes() < 10 ? '0' + (d.getMinutes()) : d.getMinutes()) + ':' + (d.getSeconds() < 10 ? '0' + (d.getSeconds()) : d.getSeconds())
    },
    // + n 天
    addDate(n) {
        const d = new Date()
        d.setDate(d.getDate() + n); // 日
        return this.currDate(d)
    }
}